package top.mcwebsite.playandroid

import android.app.Application
import android.content.Context
import android.util.Log
import top.mcwebsite.playandroid.common.api.AppService
import top.mcwebsite.playandroid.common.network.api.NetworkService
import top.mcwebsite.playandroid.common.user.api.IUserService
import top.mcwebsite.playandroid.lib.servicemanager.core.ServiceManager
import kotlin.system.measureTimeMillis
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import top.mcwebsite.playandroid.lib.task.DispatchMode
import top.mcwebsite.playandroid.lib.task.TaskManager
import top.mcwebsite.playandroid.lib.task.task

class PlayAndroidApplication : Application() {

    override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(base)

    }

    companion object Task {
        const val SERVICE_MANAGER_INIT_TASK = "ServiceManagerInitTask"
        const val NETWORK_SERVICE_INIT = "NetworkServiceInitTask"
    }

    override fun onCreate() {
        super.onCreate()
        val controller = TaskManager
            .newTaskChain()
            .addTask(task {
                name = SERVICE_MANAGER_INIT_TASK
                dispatchMode = DispatchMode.MAIN
                job = {
                    ServiceManager.init()
                }
            }).addTask(task {
                job = {
                    ServiceManager.getService<AppService>().init(this@PlayAndroidApplication)
                }
                dependsOn = listOf(SERVICE_MANAGER_INIT_TASK)
            }).addTask(task {
                name = NETWORK_SERVICE_INIT
                job = {
                    ServiceManager.getService<NetworkService>().init()
                }
                dependsOn = listOf(SERVICE_MANAGER_INIT_TASK)
            }).addTask(task {
                job = {
                    ServiceManager.getService<IUserService>().init()
                }
                dependsOn = listOf(NETWORK_SERVICE_INIT)
            }).start()

    }
}